﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace SuperDeepZoom
{

    /// <summary>
    /// An items control which allows the overlay of the fly through elements
    /// It is based on a standard ItemsControl
    /// </summary>
    [TemplateVisualState(Name="Visible", GroupName="VisibilityStates")]
    [TemplateVisualState(Name = "Invisible", GroupName = "VisibilityStates")]

    public class FlyThroughOverlay : ItemsControl
    {
        public static readonly DependencyProperty ItemContainerStyleProperty;

        FlyThroughManager manager;
        MultiScaleImage msi;
        ImageArranger arranger;

        FlyThroughOverlayManager overlayManager;

        bool overlayEnabled = false;
        bool tempDisable = false;

        public FlyThroughOverlay()
        {
            DefaultStyleKey = typeof(FlyThroughOverlay);            
            
            updateStates(false);            
        }

        void init()
        {
            if (manager != null && msi != null && arranger != null)
            {
                overlayManager = new FlyThroughOverlayManager(manager, msi, arranger);
                this.ItemsSource = overlayManager.Segments;
            }
        }

        internal FlyThroughOverlayManager FlyThroughOverlayManager
        {
            get
            {
                return overlayManager;
            }
        }

        internal FlyThroughManager FlyThroughManager
        {
            set
            {
                manager = value;
            }           
        }

        internal MultiScaleImage MultiScaleImage
        {
            set
            {
                this.msi = value;                
            }
        }

        internal ImageArranger ImageArranger
        {
            set
            {
                if (arranger != null)
                {
                    arranger.ArrangeAnimationComplete -= new EventHandler(arranger_ArrangeAnimationComplete);
                }

                arranger = value;
                arranger.ArrangeAnimationComplete += new EventHandler(arranger_ArrangeAnimationComplete);
                arranger.ImagesRearranged += new EventHandler(arranger_ImagesRearranged);
                init();
            }
        }

        #region EventHandlers

        void arranger_ImagesRearranged(object sender, EventArgs e)
        {
            tempDisable = true;
            updateStates(true);
        }

        void arranger_ArrangeAnimationComplete(object sender, EventArgs e)
        {
            if (overlayManager == null)
            {
                init();
            }
            tempDisable = false;
            updateStates(true);
        }

        #endregion

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new FlyThroughOverlayItem();
        }        

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is FlyThroughOverlayItem;
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            var flyItem = element as FlyThroughOverlayItem;

            if (ItemContainerStyle != null)
            {
                flyItem.Style = ItemContainerStyle;
            }

            base.PrepareContainerForItemOverride(element, item);
        }

        public Style ItemContainerStyle
        {
            get
            {
                return GetValue(ItemContainerStyleProperty) as Style;
            }
            set
            {
                SetValue(ItemContainerStyleProperty, value);
            }
        }

        public void ToggleOverlayEnabled(bool enabled)
        {
            overlayEnabled = enabled;
            updateStates(true);
        }

        void updateStates(bool useTrans)
        {
            if (overlayEnabled && !tempDisable)
            {
                VisualStateManager.GoToState(this, "Visible", useTrans);
            }
            else
            {
                VisualStateManager.GoToState(this, "Invisible", useTrans);
            }
        }


        static FlyThroughOverlay()
        {
            ItemContainerStyleProperty = DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(FlyThroughOverlay), null);
        }
    }
}
